﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int Add(int a, int b) {
	return a + b;
}
int Sub(int a, int b) {
	return a - b;
}
int Mul(int a, int b) {
	return a * b;
}
int Div(int a, int b) {
	if (0 == b) {
		printf("error:div zero!\n");
		return -1;
	}
	return a / b;
}
void Menu() {
	printf("*************************\n");
	printf(" 1:add           2:sub \n");
	printf(" 3:mul           4:div \n");
	printf("*************************\n");
	printf("请选择：");
}
int main() 
{
	//int (*parr1[10])();//定义一个函数指针数组
	//int* parr2[10]();错误的定义形式
	//int (*)() parr3[10];错误的定义形式

	//函数指针数组的应用:转移表（减少调用函数的重复率）如计算器
	int quit = 0;
	int select = 0;
	int (*fun[5])(int, int) = { NULL,Add,Sub,Mul,Div };
	//转移表建立映射关系的通过下标调用函数
	while (!quit) {
		Menu();
		scanf("%d", &select);
		if (select >= 1 && select <= 4) {
			int x = 0;
			int y = 0;
			printf("Please enter data first:");
			scanf("%d", &x);
			printf("Please enter data second:");
			scanf("%d", &y);
			int result = fun[select](x, y);
			printf("result:%d\n", result);
		}
		else {
			printf("error:Enter Trror,Try Again!\n");
		}
	}
	return  0;
}